package com.tws.pctask.service;

import com.tws.common.model.ResponseWrapper;
import com.tws.common.tool.CommonTool;
import com.tws.common.tool.DataProcessing;
import com.tws.dao.entity.User;
import com.tws.pctask.mapper.StorehouseManageMapper;
import com.tws.pctask.model.departmentManageModel.GetDepartmentListResult;
import com.tws.pctask.model.registerManageModel.CreatTables;
import com.tws.pctask.model.storehouseManageModel.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.GetMapping;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @Description： 仓库管理服务层
 * @Author: wbf
 * @Date: 2022/3/14 19:33
 */
@Service
public class StorehouseManageService {
    /**
     * 获取当前所登陆的用户信息
     */
    User loginUser = LoginService.getLoginUser();
    CreatTables tables = new CreatTables(loginUser.getUserId());

    /**
     * 仓库管理mapper
     */
    @Autowired
    StorehouseManageMapper storehouseManageMapper;

    /**
     * 5.1获取仓库列表
     * @param param 获取仓库列表参数 详情见该类注释
     * @return 仓库列表
     */
    public ResponseWrapper getStorehouseList(GetStorehouseListParam param){
        param.setTableName("02044f2fcd41_storehouse");
        param.setSearchKey(DataProcessing.CreatFuzzyQuery(param.getSearchKey()));
        param.setPage(DataProcessing.CalculatePage(param.getPage(),param.getPageSize()));
        List<Map<String, Object>> mapList = storehouseManageMapper.getStorehouseList(param);
        List<GetStorehouseListResult> results = CommonTool.mapListToBeanList(mapList, GetStorehouseListResult.class);
        return ResponseWrapper.markSuccess(results);
    }

    /**
     * 5.2新增或修改仓库
     * @param param 新增或修改仓库列表参数 详情见该类注释
     * @return 操作结果
     */
    public ResponseWrapper addOrUpdateStorehouse(AddOrUpdateStorehouseParam param){
        param.setTableName("02044f2fcd41_storehouse");
        storehouseManageMapper.addOrUpdateStorehouse(param);
        return ResponseWrapper.markSuccess();
    }

    /**
     * 5.3检查仓库名称是否重复
     * @param param 仓库名称
     * @return 是否重复
     */
    public ResponseWrapper checkStorehouseName(CheckStorehouseNameParam param){
        param.setTableName("02044f2fcd41_storehouse");
        Integer sum = storehouseManageMapper.checkStorehouseName(param);
        CheckStorehouseNameResult result = new CheckStorehouseNameResult();
        result.setExist(sum > 0);
        return ResponseWrapper.markSuccess(result);
    }

    /**
     * 5.4获取单个仓库信息
     * @param param 仓库id
     * @return 仓库信息
     */
    public ResponseWrapper getStorehouseInfo(GetStorehouseInfoParam param){
        param.setTableName("02044f2fcd41_storehouse");
        Map<String, Object> map = storehouseManageMapper.getStorehouseInfo(param);
        List<Map<String,Object>> resultMaps = new ArrayList<>();
        resultMaps.add(map);
        List<GetStorehouseInfoResult> results = CommonTool.mapListToBeanList(resultMaps, GetStorehouseInfoResult.class);
        return ResponseWrapper.markSuccess(results.get(0));
    }

    /**
     * 5.5删除仓库
     * @param param 仓库id
     * @return 相应消息
     */
    public ResponseWrapper delStorehouse(DelStorehouseParam param){
        param.setTableName("02044f2fcd41_storehouse");
        storehouseManageMapper.delStorehouse(param);
        return ResponseWrapper.markSuccess();
    }
}
